<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2017/11/21 0021

 * Time: 18:39

 */

namespace  app\admin\controller;

use think\Db;

use think\Controller;

use think\Session;



class Index extends Controller

{

    public function index(){

        return $this->fetch();

    }

    //登录

    public function login(){

        $username = input('post.username');

        $password = md5(input('post.password'));

        $code = input('post.code');

        $Verify = new \org\Verify();

        $Verify->check($code) or die(json_encode(['code'=>false,'msg'=>'验证码错误']));

        $where['username'] =  $username;

        $where['password'] = $password;

        $check = Db::name('admin')->where($where)->find();

        $check_status =Db::name('admin')->where($where)->where('status',1)->find();

        if(!$check){

            return json(['code'=>0,'msg'=>'账号或密码错误']);

        }

        if($check_status){

            return json(['code'=>0,'msg'=>'该账号已被禁用']);

        }



       $data['last_login_time']=time();

        $data['last_login_ip']=getIp();

        $data['is_login']=1;

        Db::name('admin')->where('id',$check['id'])->update($data);

        Db::name('admin')->where('id',$check['id'])->setInc('login_num',1);

        $check['password'] =input('post.password');

        session('admin',$check);

        session('id',$check['id']);

        session('sign','mobile');

        return json(['code'=>1,'msg'=>'登录成功']);



    }//退出登录

    public function logout(){

        $data['is_login']=0;

        Db::name('admin')->where('id',session('id'))->update($data);

        session('admin',null);

        session('id',null);

        session('sign',null);

        return json(['code'=>1,'msg'=>'退出成功']);

   }







    public function verify()

    {

        $Verify = new \org\Verify();

        $Verify->codeSet = '0123456789';

        $Verify->imageH = 40;

        $Verify->imageW = 100;

        $Verify->fontSize = 14;

        $Verify->length   = 3;

        $Verify->useCurve   = false;

        ob_clean();

        $Verify->entry();

    }

}